package megrez.bussiness.system.config;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import lombok.extern.slf4j.Slf4j;
import megrez.common.util.EntityMissingException;
import megrez.common.util.MegrezException;
import megrez.common.util.ThrowableFiltered;

/**
 * 统一异常处理
 *  
 * @author Lucky Yang
 * @since 0.0.1
 */
@RestControllerAdvice
@Slf4j
public class ControllerExceptionHandler {
    @ExceptionHandler(EntityMissingException.class)
    public ResponseEntity<?> handleEntityMissingException(EntityMissingException e) {
        log.warn("业务异常 {}", ThrowableFiltered.INSTANCE.renderLine(e));

        HttpStatus status = HttpStatus.NOT_FOUND;
        return ResponseEntity.status(status).body(e.generateMap());
    }

    @ExceptionHandler(MegrezException.class)
    public ResponseEntity<?> handleEisException(MegrezException e) {
        log.warn("业务异常 {}", ThrowableFiltered.INSTANCE.renderLine(e));

        HttpStatus status = HttpStatus.BAD_REQUEST;
        return ResponseEntity.status(status).body(e.generateMap());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> handleExcepiton(Exception e) {
        log.error("系统异常", e);

        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return ResponseEntity.status(status).body(e.getMessage());
    }  
}
